//获取应用实例
var WxParse = require('../../wxParse/wxParse.js');
let time = require('../util/date.js');
const app = getApp();
Page({
	data: {
    id:'',
    goodsMsg:[],
    swiperImgList:[
      ],
    imglist:[],
    hasImgList:false,
    skulist: [],
    commentList: [],
    commentStatistics:[],
    addressList:[],
    goods_id:'',
    is_collect:false,
    showModalStatus: false,//是否显示
    hasSku:false,
    gg_id: 0,//规格ID
    gg_txt: '',//规格文本
    gg_price: 0,//规格价格
    guigeList: [{ guige: '100', price: '150' }, { guige: '200', price: '150' }, { guige: '300', price: '150' }],
    num: 1,//初始数量
    hasYhq:false,
    /**
        * 页面配置
        */
    currentTab: 0,
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  onReady(){
    this.getDeviceInfo();
  },
	onLoad: function(options) {
    var that=this;
    that.setData({
      goods_id:options.id
    })
    that.getGoodById(options.id);
  },

  onShow:function(){
    var that=this;
  },
  //获取商品详情
  getGoodById(id){
    wx.showLoading({
      title: '商品加载中',
      mask: false
    })
    var that = this;
    var jlV;
    var c1;
    var qhprice;//券后价
    wx.request({
      url: app.globalData.api + '/app.php?c=Pdd&a=getGoodsDetail',
      data:
      {
        goods_id:id,
        token:app.globalData.token
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if(res.data.code==0){
          
          var tempimg;
          jlV = res.data.data.commission_vip / 100
          c1 = res.data.data.goods_details.commission / 100
          for (var i = 0; i < res.data.data.goods_details.goods_gallery_urls.length; i++) {
            tempimg = tempimg + '< img src = ' + res.data.data.goods_details.goods_gallery_urls[i] + ' style = "max-width:100%" />'
          }
          that.setData({
            tempimg: "<html>" + tempimg + "</html>"
          })

          var mingroupPrice = res.data.data.goods_details.min_group_price;
          var coupon_discount = res.data.data.goods_details.coupon_discount;
          var promotion_rate = res.data.data.goods_details.promotion_rate;
          var sum0 = mingroupPrice - coupon_discount;
          var sum1 = sum0 * promotion_rate / 1000;
          var sum3 = sum1 / 100
          console.log(mingroupPrice)
          console.log(coupon_discount)
          console.log(promotion_rate)
          console.log(sum1)
          console.log(sum3)

          // 时间戳转化的日期格式： 2019-02-21 10:04:38
          that.setData({
            goodsList: res.data.data.goods_details,
            commission_vip: jlV.toFixed(2),
            group_id: app.globalData.group_id,
            ysqdate: time.formatTimeTwo(res.data.data.goods_details.coupon_end_time, 'Y-M-D h:m:s')
          })
          if (app.globalData.group_id == 1) {//会员
            that.setData({
              commission: c1.toFixed(2),
            })
          } else {//淘掌柜
            that.setData({
              commission: jlV.toFixed(2),
            })
          }
          console.log(that.data.ysqdate)
        }else {
          wx.showToast({
            title: ''+res.data.msg,
            icon:'none'
          })
          if(res.data.code==105){
            wx.reLaunch({
              url: '/pages/mine/login',
            })
          }
        }
        that.getMerchantMsg(that.data.goodsMsg.mall_id);
      }, fail: function () {

      }
    });
  },

  onShareAppMessage: function (e) {
    if (e.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(e.target);
      if (e.target.dataset.index == 1) {
        return {
          title: '快来汇客熊小程序领取优惠券啦',
          desc: '',
          imageUrl: "/static/images/splash.png",
          path: '/pages/live/splash' // 路径，传递参数到指定页面。
        }
      } else {
        return {
          title: '【拼多多】  优惠券' + this.data.goodsList.coupon_discount / 100 + '元券  原价￥' + this.data.goodsList.min_group_price / 100 + '  券后价￥' +
            (this.data.goodsList.min_group_price - this.data.goodsList.coupon_discount) / 100,
          desc: '',
          path: '/pages/live/splash?auth_code=' + app.globalData.auth_code + '&goods_id=' + this.data.goodsList.goods_id // 路径，传递参数到指定页面。
        }
      }
    } else {
      return {
        title: '【拼多多】  优惠券' + this.data.goodsList.coupon_discount / 100 + '元券  原价￥' + this.data.goodsList.min_group_price / 100 + '  券后价￥' +
          (this.data.goodsList.min_group_price - this.data.goodsList.coupon_discount) / 100,
        desc: '',
        path: '/pages/live/splash?auth_code=' + app.globalData.auth_code + '&goods_id=' + this.data.goodsList.goods_id // 路径，传递参数到指定页面。
      }
    }
  },
  downloadImg: function () {
    wx.navigateTo({
      url: '/pages/mine/downimg',
    })
  },
  //预览图片
  previewImage: function (e) {
    console.log(e)
    var imgUrl = e.currentTarget.dataset.src;
    wx.previewImage({
      urls: [imgUrl], //需要预览的图片http链接列表，注意是数组
      current: '', // 当前显示图片的http链接，默认是第一个
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  createShared: function () {
    wx.navigateTo({
      url: '/pages/shop/create-shared?goodid=' + this.data.goodsList.goods_id,
    })
  },
  //查询店铺信息
  getMerchantMsg(mall_id) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Pdd&a=getMerchantMsg',
      data:
      {
        mall_id: mall_id
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        that.setData({
          merchantMsg: res.data.data.merchantMsg
        })
      }, fail: function () {

      }
    });
  },

  toGoodDetail:function(e){
    console.log(e)
    wx.navigateTo({
      url: 'item?id='+e.currentTarget.id,
    })
  },

  //分享
  shareds: function () {
    var that = this;
    wx.navigateToMiniProgram({
      appId: 'wx32540bd863b27570',
      path: 'package_a/welfare_coupon/welfare_coupon?goods_id=' + that.data.goods_id + '&pid=8792163_102229125&customParameters=65880&authDuoId=200005&cpsSign=CC_200901_8792163_102229125_02a3fb793ae17af26b7747217ebdb239&duoduo_type=2',
      success(res) {
        console.log(res)
      }
    })
  },

  //领券
  getQuan: function () {
    var that = this;
    wx.navigateToMiniProgram({
      appId: 'wx32540bd863b27570',
      path: 'package_a/welfare_coupon/welfare_coupon?goods_id=' + that.data.goods_id + '&pid=8792163_102229125&customParameters=65880&authDuoId=200005&cpsSign=CC_200901_8792163_102229125_02a3fb793ae17af26b7747217ebdb239&duoduo_type=2',
      success(res) {
        console.log(res)
      }
    })
  },

  //查看图文详情
  toContent:function(e){
    console.log(e)
    wx.navigateTo({
      url: 'webview?url='+e.currentTarget.dataset.url,
    })
  },

});
